using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ebookreader
{
    public class VirtualKeyboard: Menu
    {
        #region Fields

        //SpriteBatch sprite_batch;
        TextEdit text_edit;
        //List<MenuEntry> entries;
        List<RenderableObject> renderables;

        #endregion

        #region Constructors
        public VirtualKeyboard(Game game ,SpriteBatch sprite_batch, SpriteFont font, TextEdit text_edit, Texture2D key_box, 
            string[] keys_label, int key_width, int key_height, int x_coord, int y_coord, 
            int x_spacing, int y_spacing, int rows_n, Action <string> onSubmit, IInputManager input_manager)
            :base(game, input_manager)
        {
            this.sprite_batch = sprite_batch;
            this.text_edit = text_edit;
            Game.Components.Add(this);
            //entries = new List<MenuEntry>();
            renderables = new List<RenderableObject>();
            var currentState = Game.Services.GetService(typeof(State)) as State;
            int colums = keys_label.Length / rows_n + ((keys_label.Length % rows_n>0)?1:0);

            for (int i = 0; i < rows_n; i++)
            {
                for (int j = 0; j < Math.Min(colums, keys_label.Length - (i)*colums); j++)
                {
                    BoxedLabelButton b = new BoxedLabelButton(sprite_batch, key_box,
                        new Vector2(x_coord + (key_width + x_spacing) * j, y_coord + (key_height + y_spacing) * i),
                        font, keys_label[i * colums + j], Color.White, true, currentState.options);
                    b.rectangle = new Rectangle(
                        (int)(x_coord + (key_width + x_spacing) * j),
                        (int)(y_coord + (key_height + y_spacing) * i), key_width, key_height);


                    if (keys_label[i * colums + j].Equals("<-"))
                    {
                        b.OnPress += new Press((MenuEntry m) => { text_edit.RemoveChar(); });
                    }
                    else if (keys_label[i * colums + j].Equals("OK"))
                    {
                        b.OnPress += new Press((MenuEntry m) => { 
                            
                            onSubmit(text_edit.text);
                        Game.Components.Remove(this);
                        Dispose();
                        });

                    }
                    else
                        b.OnPress += new Press((MenuEntry m) => { text_edit.AddChar(b.Label); });

                    entries.Add(b);
                }
            }

        }

        #endregion



        public override void Initialize()
        {
            //Game.Components.Add(new InputManager(Game));
            //input_manager = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            DrawOrder = 5;
            input_manager.OnTap += new InputManager.Tap(OnTap);
            base.Initialize();
        }


        public override void Draw(GameTime gameTime)
        {
            sprite_batch.Begin();
            DrawInsideSpriteBatch(gameTime);
            sprite_batch.End();
            base.Draw(gameTime);
        }

        #region Draw Methods

        public void DrawInsideSpriteBatch(GameTime gameTime)
        {
            //for (int i = 0; i < renderables.Count; i++)
            //{
            //    renderables[i].DrawAfterBegin();
            //}

            //for (int i = 0; i < entries.Count; i++)
            //{
            //    entries[i].DrawInsideSpriteBatch();
            //}

            text_edit.DrawInsideSpriteBatch(gameTime);
        }

        public void DrawOutsideSpriteBatch(GameTime gameTime)
        {
            sprite_batch.Begin();

            DrawInsideSpriteBatch(gameTime);

            sprite_batch.End();
        }

        #endregion

        #region Proprieties

        public TextEdit TextEdit { get { return text_edit; } }

        #endregion
    }
}
